package ref;

import org.junit.Test;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Author com.alibaba.lizhen
 * @Date 2021/3/23 10:24 下午
 * @Version 1.0
 * @Description
 *
 * 构造器引用
 * 格式：
 *      ClassName :: new
 * 注意：**需要调用的构造器的参数列表要与函数时接口中抽象方法的参数列表保持一致
 */
public class ConstructRefTest {

    @Test
    public void test() {

        // 构造方法引用  类名::new
        Supplier<Employee> sup = () -> new Employee();
        System.out.println(sup.get());
        Supplier<Employee> sup2 = Employee::new;
        System.out.println(sup2.get());

        // 构造方法引用 类名::new （带一个参数）
        Function<Integer, Employee> fun1 = (x) -> new Employee(x);
        Function<Integer, Employee> fun2 = Employee::new;
        System.out.println(fun2.apply(100));

        Function<String, Employee> fun3 = (str) -> new Employee(str);
        Function<String, Employee> fun4 = Employee::new;
        System.out.println(fun4.apply("张三"));


        //带两个参数的构造器
        BiFunction<Integer,String,Employee> fun5 = (x,str) -> new Employee(x,str);
        BiFunction<Integer,String,Employee> fun6 = Employee::new;
        System.out.println(fun6.apply(200,"李四"));
    }

}
